﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Com.DA;
using ColdStorage.DOL;
using System.Media;
using System.Windows.Controls.Primitives;

namespace ColdStorage.GUI
{
    /// <summary>
    /// Interaction logic for ucAirFlowCold.xaml
    /// </summary>
    public partial class ucProduct : UserControl
    {
        public string key = "";

        /// Deleegate truyền nhiệt độ nhập vào của sản phẩm
        public delegate void GetProductInputTemp(int iProductInputTemp);
        public GetProductInputTemp myProductInputTemp;

        /// Delegate truyền nhiệt độ lưu trữ của sản phẩm
        public delegate void GetProductStorageTemp(int iProductStorageTemp);
        public GetProductStorageTemp myProducStorageTemp;


        //Delegate truyền đối tượng product
        public delegate void GetProduct(Product iproduct);
        public GetProduct myProduct;

        public int pId;
        public double RoomCapacity;
        public double dbwrappingRate;

        Product pro;

        public ucProduct()
        {
            InitializeComponent();
            LoadProductInfo();            
        }

        public void LoadProductInfo()
        {
            //Load Nhiệt độ
            cbxImportTemp.Items.Clear();                        
            cbxImportTemp.ItemsSource = LibraryManagerInCom.ProductEnthalpyList;
            cbxImportTemp.DisplayMemberPath = "temperature";
            cbxImportTemp.SelectedIndex = -1;

            cbxStorageTemp.Items.Clear();
            cbxStorageTemp.ItemsSource = LibraryManagerInCom.ProductEnthalpyList;
            cbxStorageTemp.DisplayMemberPath = "temperature";
            cbxStorageTemp.SelectedIndex = -1;

            //Load Bao bì
            cbxWrappingType.Items.Clear();            
            cbxWrappingType.ItemsSource = LibraryManagerInCom.WrappingList;
            cbxWrappingType.DisplayMemberPath = "wrapping_name";
            cbxWrappingType.SelectedIndex = -1;

            //Load Combo Info
            // Load thông tin cho combobox hệ số quay vòng hàng
            cbxRotation.Items.Clear();
            for (int i = 5; i <= 10; i++)
            {
                cbxRotation.Items.Add(i);
            }
            cbxRotation.SelectedIndex = -1;

            //Load thông tin cho hệ số nhập hàng không đồng đều
            double dbImportUneven = 0;
            for (int j = 0; j < 8; j++)
            {
                dbImportUneven += 0.5;
                cbxImportUneven.Items.Add(dbImportUneven);
            }
            cbxImportUneven.SelectedIndex = -1;
        }

        #region PROCESS PRODUCT
        //Tính toán tỷ lệ tương ứng giữa bao bì và khối lượng sản phẩm đưa vào
        public double Wrapping_rate()
        {
            return dbwrappingRate = Convert.ToDouble(((tb_wrapping)cbxWrappingType.SelectedItem).weight_rate);
        }

        // sự kiện load entanpy tương ứng khi thay đổi nhiệt độ nhập của từng sản phẩm
        private void cbxImportTemp_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxImportTemp.SelectedItem != null)
            {
                tb_enthalpy enthalpy = (tb_enthalpy)cbxImportTemp.SelectedItem;
                tb_enthalpy en = LibraryManagerInCom.ProductEnthalpyList.SingleOrDefault
                   (s => s.product_id == pId && s.temperature == enthalpy.temperature);
                if (en != null)
                {
                    txtImportEnthalpy.Text = en.enthalpy.ToString();
                }
                myProductInputTemp(Convert.ToInt32(enthalpy.temperature));
                btnApplyProduct.IsEnabled = true;
            }
        }

        //sự kiện load entanpy tương ứng khi thay đổi nhiệt độ lưu trữ của từng sản phẩm
        private void cbxStorageTemp_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxStorageTemp.SelectedItem != null)
            {
                tb_enthalpy enthalpy = (tb_enthalpy)cbxStorageTemp.SelectedItem;
                tb_enthalpy en = LibraryManagerInCom.ProductEnthalpyList.SingleOrDefault
                   (s => s.product_id == pId && s.temperature == enthalpy.temperature);
                if (en != null)
                {
                    txtStorageEnthalpy.Text = en.enthalpy.ToString();
                }
                myProducStorageTemp(Convert.ToInt32(enthalpy.temperature));
                btnApplyProduct.IsEnabled = true;
            }
        }

        //Hiển thị nhiệt dung riêng của bao bì khi thay đổi từng loại bao bì
        private void cbxWrappingType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxWrappingType.SelectedItem != null)
            {
                txtWrappingSpecificHeat.Text = ((tb_wrapping)cbxWrappingType.SelectedItem).specific_heat.ToString();
                loadWrapping();
                btnApplyProduct.IsEnabled = true;
            }
        }

        //Tính toán khối lượng bao bì theo kèm
        public void loadWrapping()
        {
            if (pro != null)
            {
                if (cbxWrappingType.SelectedItem != null)
                {
                    pro.dbProductVolumeDailyImport = Convert.ToDouble(txtVolumeDailyImport.Text);
                    txtWrappingInputVolume.Text = pro.CalculateWrappingVolume(Wrapping_rate()).ToString();
                }
            }
        }

        #endregion

        private void btnApplyProduct_Click(object sender, RoutedEventArgs e)
        {
            ApplyProduct();
        }

        public void ApplyProduct()
        {
            pro.dbProductImportTemp = Convert.ToDouble(cbxImportTemp.Text);
            pro.dbProductStorageTemp = Convert.ToDouble(cbxStorageTemp.Text);
            pro.dbProductRotationCoefficient = Convert.ToDouble(cbxRotation.Text);
            pro.dbProductImportUnevenCoefficient = Convert.ToDouble(cbxImportUneven.Text);
            pro.dbProductVolumeDailyImport = Convert.ToDouble(txtVolumeDailyImport.Text);
            pro.dbEnthalpyAfter = Convert.ToDouble(txtStorageEnthalpy.Text);
            pro.dbEnthalpyBefore = Convert.ToDouble(txtImportEnthalpy.Text);
            pro.iWrappingType = cbxWrappingType.SelectedIndex;
            pro.dbWrappingThermalCapacity = Convert.ToDouble(txtWrappingSpecificHeat.Text);
            pro.dbOriginalTemp = Convert.ToDouble(txtWrappingOrginalTemp.Text);
            pro.dbWrappingInputVolume = Convert.ToDouble(txtWrappingInputVolume.Text);
            pro.dbWrappingRate = dbwrappingRate;
            btnApplyProduct.IsEnabled = false;
        }

        public void loadProduct(Product p)
        {
            if (pro != null)
            {
                ApplyProduct();
            }
            pro = p;
            cbxImportTemp.Text = pro.dbProductImportTemp.ToString();
            cbxStorageTemp.Text = pro.dbProductStorageTemp.ToString();
            txtImportEnthalpy.Text = pro.dbEnthalpyBefore.ToString();
            txtStorageEnthalpy.Text = pro.dbEnthalpyAfter.ToString();
            cbxRotation.Text = pro.dbProductRotationCoefficient.ToString();
            cbxImportUneven.Text = pro.dbProductImportUnevenCoefficient.ToString();
            txtVolumeDailyImport.Text = pro.dbProductVolumeDailyImport.ToString();
            cbxWrappingType.SelectedIndex = pro.iWrappingType;
            txtWrappingSpecificHeat.Text = pro.dbWrappingThermalCapacity.ToString();
            txtWrappingOrginalTemp.Text = pro.dbOriginalTemp.ToString();
            txtWrappingInputVolume.Text = pro.dbWrappingInputVolume.ToString();
        }

        #region CHECK EMPTY
        public void CheckEmpty(TextBox txt)
        {
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        private void txtVolumeDailyImport_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtVolumeDailyImport);
            loadWrapping();
        }

        private void txtWrappingSpecificHeat_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWrappingSpecificHeat);
        }

        private void txtWrappingOrginalTemp_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWrappingOrginalTemp);
        }

        private void txtWrappingInputVolume_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWrappingInputVolume);
        }

        private void txtImportEnthalpy_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtImportEnthalpy);
        }

        private void txtStorageEnthalpy_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtStorageEnthalpy);
        }

        private void cbxRotation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyProduct.IsEnabled = true;
        }

        private void cbxImportUneven_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyProduct.IsEnabled = true;
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
                btnApplyProduct.IsEnabled = true;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (sender is TextBox)
                    {
                        if (!((TextBox)sender).Text.Contains("."))
                        {
                            e.Handled = false;
                            btnApplyProduct.IsEnabled = true;
                        }
                        else
                        {
                            e.Handled = true;
                            SystemSounds.Beep.Play();
                        }
                    }
                    else
                    {
                        if (sender is ComboBox)
                        {
                            if (!((ComboBox)sender).Text.Contains("."))
                            {
                                e.Handled = false;
                                btnApplyProduct.IsEnabled = true;
                            }
                            else
                            {
                                e.Handled = true;
                                SystemSounds.Beep.Play();
                            }
                        }
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }

        private void cbxRotation_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (cbxRotation.Text == "")
            {
                cbxRotation.Text = "0";
            }
            if (cbxRotation.Text.Contains(' '))
            {
                cbxRotation.Text = cbxRotation.Text.Remove(cbxRotation.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (cbxRotation.Text.First() == '.')
            {
                cbxRotation.Text = cbxRotation.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        private void cbxImportUneven_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (cbxImportUneven.Text == "")
            {
                cbxImportUneven.Text = "0";
            }
            if (cbxImportUneven.Text.Contains(' '))
            {
                cbxImportUneven.Text = cbxImportUneven.Text.Remove(cbxImportUneven.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (cbxImportUneven.Text.First() == '.')
            {
                cbxImportUneven.Text = cbxImportUneven.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }
        #endregion

        #region GOT FOCUS
        private void txtInput_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            key = txt.Name;
        }

        private void txtInput_Loaded(object sender, RoutedEventArgs e)
        {
            ToolTip tt = new ToolTip();
            TextBox txt = (TextBox)sender;
            ToolTipService.SetPlacement(tt, PlacementMode.Left);
            tt.Content = "Press F1 for Help";
            txt.ToolTip = tt;
        }
        #endregion
    }
}
